package com.gzpi.hwgggh.service.impl;

import com.alibaba.fastjson.JSON;
import com.gzpi.hwgggh.entity.*;
import com.gzpi.hwgggh.mapper.*;
import com.gzpi.hwgggh.service.DWGService;
import com.gzpi.hwgggh.sysenum.EnumErrorCode;
import com.gzpi.hwgggh.utils.*;
import com.gzpi.hwgggh.vo.SearchConditionVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date : 2020-12-21 17:22
 **/
@Service
@Transactional
public class DWGServiceImpl implements DWGService {

    @Autowired
    private DWGMapper dwgMapper;

    @Autowired
    private GGPMapper ggpMapper;

    @Autowired
    private DWTPMapper dwtpMapper;

    @Autowired
    private DWXMapper dwxMapper;

    @Autowired
    private DWCMapper dwcMapper;

    @Override
    public Result selectDWGList(SearchConditionVo searchConditionVo) {
        List<DWX> dwxs = dwxMapper.selectDWXListBySearchVo(searchConditionVo); //查询现状列表
        if (dwxs.size()>0){
            return Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("查询规划点列表成功"),dwxs);
        }
        return Result.createError(EnumErrorCode.ERROR_OBJECT_SEARCH.toString("暂无数据"));
    }

    @Override
    public Result selectDWG(String id,String num) {
        Map<String,Object> map = new HashMap<>();
        map.put("ID",id);
        DWX dwx = dwxMapper.selectDWXGBandGgpXGListAndDwtpByMap(map); //获取 现状信息 现状广告牌集合 点位图片对象
        if (dwx!=null){
            List<GGP> ggps = dwx.getXzggps();
            dwx.setIDNAME(dwx.getXSDWBH()+"-(X:0)");
            if (ggps!=null && ggps.size()>0){

                //保存相同内容map
                Map<String,Object> szwzmap = new HashMap<>();
                Map<String,Object> gyxsmap = new HashMap<>();
                Map<String,Object> ggccmap = new HashMap<>();
                Map<String,Object> zywtmap = new HashMap<>();
                //拼接后显示字符串
                String szwz = "";
                String gyxs = "";
                String ggcc = "";
                String zywt = "";
                //筛选相同内容合并编号
                for (GGP ggp : ggps) {
                    String ggpbh = ggp.getGGBH().substring(ggp.getGGBH().indexOf("_")+1);
                    if (StringUtils.isNotBlank(ggp.getSZWZ())) {
                        if (ggp.getSZWZ().contains("其他")  && StringUtils.isNotBlank(ggp.getQTWZ())){
                            CommonTools.getMapData(szwzmap, ggp.getQTWZ(), ggpbh);
                        }else {
                            CommonTools.getMapData(szwzmap, ggp.getSZWZ(), ggpbh);
                        }
                    }
                    if (StringUtils.isNotBlank(ggp.getGYXS())) {
                        CommonTools.getMapData(gyxsmap, ggp.getGYXS(), ggpbh);
                    }
                    if (StringUtils.isNotBlank(ggp.getGGCC())) {
                        String[] ggcc_str = ggp.getGGCC().split("×");
                        Double temp_1 = Double.valueOf(ggcc_str[0]);
                        if(ggcc_str[0].indexOf(".")>0){
                            temp_1 = Double.valueOf(ggcc_str[0].substring(0,ggcc_str[0].indexOf(".")+2));
                        }
                        Double temp_2 = Double.valueOf(ggcc_str[1].substring(0,ggcc_str[1].indexOf("=")));
                        if(ggcc_str[1].indexOf(".")>0){
                            temp_2 = Double.valueOf(ggcc_str[1].substring(0,ggcc_str[1].indexOf(".")+2));
                        }
                        DecimalFormat    df   = new DecimalFormat("######0.0");
                        String temp_3 = temp_1+"×"+temp_2+"="+ df.format(temp_1*temp_2)+"㎡";
                        CommonTools.getMapData(ggccmap,temp_3, ggpbh);
                    }
                    if(StringUtils.isNotBlank(ggp.getZYWT())) {
                        if (ggp.getZYWT().contains("其他") && StringUtils.isNotBlank(ggp.getQTWT())){
                            CommonTools.getMapData(zywtmap, ggp.getQTWT(), ggpbh);
                        }else {
                            CommonTools.getMapData(zywtmap, ggp.getZYWT(), ggpbh);
                        }
                    }
                }

                //排序拼接显示字符串
                szwz = CommonTools.mapSort(szwzmap);
                gyxs = CommonTools.mapSort(gyxsmap);
                ggcc = CommonTools.mapSort(ggccmap);
                zywt = CommonTools.mapSort(zywtmap);

                Map<String, Object> ggpmap = new HashMap<>();
                ggpmap.put("szwz",szwz);
                ggpmap.put("gyxs",gyxs);
                ggpmap.put("ggcc",ggcc);
                ggpmap.put("zywt",zywt);

                dwx.setGgpmap(ggpmap);
                dwx.setIDNAME(dwx.getXSDWBH()+"-(X:"+ggps.size()+")");
            }

            DWG dwg = dwx.getDwg();
            if(dwg!=null){
                List<GGP> dwgggps = dwx.getGhggps();
                dwg.setIDNAME(dwx.getXSDWBH()+"-(G:0)");
                if (dwgggps!=null && dwgggps.size()>0){

                    //同上
                    Map<String,Object> ghmapszwz = new HashMap<>();
                    Map<String,Object> ghmapnrlx = new HashMap<>();
                    Map<String,Object> ghmapgyxs = new HashMap<>();
                    Map<String,Object> ghmapggcc = new HashMap<>();
                    Map<String,Object> ghmapcl = new HashMap<>();
                    Map<String,Object> ghmapzm = new HashMap<>();
                    Map<String,Object> ghmapsc = new HashMap<>();
                    //同上
                    String ghszwz="";
                    String ghnrlx="";
                    String ghgyxs="";
                    String ghggcc="";
                    String ghcl="";
                    String ghzm="";
                    String ghsc="";
                    //同上
                    for (GGP ggp : dwgggps) {
                        String ggpbh = ggp.getGGBH().substring(ggp.getGGBH().indexOf("_")+1);
                        if (StringUtils.isNotBlank(ggp.getSZWZ())) {
                            if (ggp.getSZWZ().contains("其他") && StringUtils.isNotBlank(ggp.getQTWZ())) {
                                CommonTools.getMapData(ghmapszwz, ggp.getQTWZ(), ggpbh);
                            }else{
                                CommonTools.getMapData(ghmapszwz, ggp.getSZWZ(), ggpbh);
                            }
                        }
                        if (StringUtils.isNotBlank(ggp.getNRLX())) {
                            CommonTools.getMapData(ghmapnrlx, ggp.getNRLX(), ggpbh);
                        }
                        if (StringUtils.isNotBlank(ggp.getGYXS())) {
                            if (ggp.getGYXS().contains("其他") && StringUtils.isNotBlank(ggp.getQTGY())) {
                                CommonTools.getMapData(ghmapgyxs, ggp.getQTGY(), ggpbh);
                            }else{
                                CommonTools.getMapData(ghmapgyxs, ggp.getGYXS(), ggpbh);
                            }
                        }
                        if (StringUtils.isNotBlank(ggp.getGGCC())) {
                            String[] ggcc_str = ggp.getGGCC().split("×");
                            Double temp_1 = Double.valueOf(ggcc_str[0]);
                            if(ggcc_str[0].indexOf(".")>0){
                                temp_1 = Double.valueOf(ggcc_str[0].substring(0,ggcc_str[0].indexOf(".")+2));
                            }
                            Double temp_2 = Double.valueOf(ggcc_str[1].substring(0,ggcc_str[1].indexOf("=")));
                            if(ggcc_str[1].indexOf(".")>0){
                                temp_2 = Double.valueOf(ggcc_str[1].substring(0,ggcc_str[1].indexOf(".")+2));
                            }
                            DecimalFormat    df   = new DecimalFormat("######0.0");
                            String temp_3 = temp_1+"×"+temp_2+"="+ df.format(temp_1*temp_2)+"㎡";
                            CommonTools.getMapData(ghmapggcc, temp_3, ggpbh);
                        }
                        if (StringUtils.isNotBlank(ggp.getCLYD())) {
                            if (ggp.getCLYD().contains("其他") && StringUtils.isNotBlank(ggp.getQTCL())) {
                                CommonTools.getMapData(ghmapcl, ggp.getQTCL(), ggpbh);
                            }else{
                                CommonTools.getMapData(ghmapcl, ggp.getCLYD(), ggpbh);
                            }
                        }
                        if (StringUtils.isNotBlank(ggp.getZM())) {
                            CommonTools.getMapData(ghmapzm, ggp.getZM(), ggpbh);
                        }
                        if (StringUtils.isNotBlank(ggp.getSC())) {
                            CommonTools.getMapData(ghmapsc, ggp.getSC(), ggpbh);
                        }
                    }
                    //同上
                    ghszwz = CommonTools.mapSort(ghmapszwz);
                    ghnrlx = CommonTools.mapSort(ghmapnrlx);
                    ghgyxs = CommonTools.mapSort(ghmapgyxs);
                    ghggcc = CommonTools.mapSort(ghmapggcc);
                    ghcl = CommonTools.mapSort(ghmapcl);
                    ghzm = CommonTools.mapSort(ghmapzm);
                    ghsc = CommonTools.mapSort(ghmapsc);

                    Map<String, Object> ggpmap = new HashMap<>();
                    ggpmap.put("szwz",ghszwz);
                    ggpmap.put("gyxs",ghgyxs);
                    ggpmap.put("ggcc",ghggcc);
                    ggpmap.put("nrlx",ghnrlx);
                    ggpmap.put("cl",ghcl);
                    ggpmap.put("zm",ghzm);
                    ggpmap.put("sc",ghsc);

                    dwg.setGgpmap(ggpmap);
                    dwg.setGgps(dwgggps);
                    dwg.setIDNAME(dwx.getXSDWBH()+"-(G:"+dwgggps.size()+")");
                }
                dwx.setDwg(dwg);
            }else {
                dwx.getDwg().setID(CommonTools.getCurrentTimeMillis());
                dwx.getDwg().setFID(dwx.getID());
            }
            return Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("成功"),dwx);
        }else{
            return Result.createError(EnumErrorCode.ERROR_OBJECT_SEARCH.toString());
        }
    }

    @Override
    public Result updateDWG(MultipartFile[] files,String dwxJson, HttpServletRequest request) throws Exception{
        DWX dwx = null;
        if (StringUtils.isNotBlank(dwxJson)){
            dwx = JSON.parseObject(dwxJson,DWX.class);//json数据转对象
        }
        boolean isSuccess = false; //图片是否上传成功
        Map<String,Object> map = new HashMap<>();//条件
        String ID = CommonTools.getCurrentTimeMillis();//新点位ID
        if (dwx!=null) {
            //编辑现状信息
            if (dwx!=null){
                map.put("ID",dwx.getID());
                if (dwxMapper.selectDWXCountByMap(map)>0) {
                    dwx.setMTIME(CommonTools.getCurrentTime());
                    dwxMapper.updateDWX(dwx);
                }else{
                    dwx.setID(ID);
                    dwx.setCTIME(CommonTools.getCurrentTime());
                    dwxMapper.insertDWX(dwx);
                }
            }
            //编辑规划信息
            DWG dwg = dwx.getDwg();
            if (dwg!=null){
                map.put("FID",dwg.getFID());
                DWTP dwtp = dwtpMapper.selectDWTPbyMap(map); //获取已有点位图片对象
                if (dwtp==null) dwtp = new DWTP();
                if(files.length>0){
                    for (MultipartFile file : files) { //编辑文件集合
                        CommonTools.uploadFile(file,dwtp,dwx.getID(),request);
                    }
                    isSuccess = true;
                }
                if (isSuccess){
                    if (dwtpMapper.selectDWTPCountbyMap(map)>0){
                        dwtpMapper.updateDWTP(dwtp);
                    }else{
                        dwtp.setID(CommonTools.getCurrentTimeMillis());
                        dwtp.setFID(dwg.getFID());
                        dwtpMapper.insertDWTP(dwtp);
                    }
                }
                map.put("ID",dwg.getID());
                map.put("FID",dwg.getFID());
                if (dwgMapper.selectDWGcountByMap(map)>0){
                    dwg.setMTIME(CommonTools.getCurrentTime());
                    dwgMapper.updateDWGByDWBH(dwg);
                }else{
                    dwg.setID(CommonTools.getCurrentTimeMillis());
                    dwg.setFID(dwx.getID());
                    dwg.setCTIME(CommonTools.getCurrentTime());
                    dwgMapper.insertDWG(dwg);
                }
            }
            return Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("保存成功"));
        }
        return Result.createError(EnumErrorCode.ERROR_OBJECT_ADD.toString());
    }

}
